<template>
  <dtDialog
    title="三方角色导入"
    :visible.sync="fileUploadVisible"
    width="620px"
    :is-loading="uploadLoading"
    @closeDialog="cancel"
    @comfirmBtn="importDataSave"
  >
    <el-form slot="content">
      <div class="">
        <dt-importFile ref="dtImportFileRef" :down-load-template="handleImportTemplate" :type="fileName" />
      </div>
    </el-form>
  </dtDialog>
</template>
<script>
import { downloadTemplate, uploadRoles } from '@/framework/api/authCenter/bussinessRoleManage'

export default {
  data() {
    return {
      uploadLoading: false,
      fileUploadVisible: false,
      handleImportTemplate: downloadTemplate,
      fileName: '三方角色导入'
    }
  },
  methods: {
    openImportDialog() {
      this.fileUploadVisible = true
    },
    importDataSave() {
      const file = this.$refs.dtImportFileRef.fileList
      if (file.length === 0) {
        this.$dtMessage({
          title: '失败',
          message: '请选择需要上传的文件',
          type: 'error',
          duration: 2000
        })
        return
      }
      const formData = new FormData()
      // 数据
      formData.append('file', file[0].raw)
      formData.append('name', file[0].name)
      uploadRoles(formData).then(async (res) => {
        this.uploadLoading = true
        this.fileUploadVisible = false
        if (res.data.errorDownloadPath) {
          this.$alert(`<div> <div class="title">${res.data.tip}</div> <div class="content"><span class="btn" id="messageBtn" style="color:#5688E8;cursor:pointer">点击下载导入失败三方角色信息</span> </div> </div>`, '导入完成', {
            dangerouslyUseHTMLString: true
          })
        } else {
          this.$alert(`<div> <div class="title">${res.data.tip}导入完成`, {
            dangerouslyUseHTMLString: true
          })
        }
        this.$parent.getList(1)
        await this.$nextTick()
        document.getElementById('messageBtn').onclick = function () {
          window.location.href = res.data.errorDownloadPath
        }
        this.uploadLoading = false
      }).catch((res) => {
        this.uploadLoading = false
      })
    },
    cancel() {
      this.fileUploadVisible = false
      this.$refs.dtImportFileRef.fileList = []
    }
  }
}
</script>
